#pragma once

#include "my_defs.h"
#include "comm/my_str.h"
#include <yaml.h>

static inline my_bool_t
my_yaml_node_int64(yaml_node_t *node, int64_t *ret)
{
    if (node->type != YAML_SCALAR_NODE) {
        return MY_FALSE;
    }

    my_str_t str_val;
    str_val.len = (int64_t)node->data.scalar.length;
    str_val.data = (char *)node->data.scalar.value;

    return my_str_to_int64(&str_val, ret);
}

static inline my_bool_t
my_yaml_node_double(yaml_node_t *node, double *ret)
{
    if (node->type != YAML_SCALAR_NODE) {
        return MY_FALSE;
    }

    my_str_t str_val;
    str_val.len = (int64_t)node->data.scalar.length;
    str_val.data = (char *)node->data.scalar.value;

    return my_str_to_double(&str_val, ret);
}

static inline my_bool_t
my_yaml_node_bool(yaml_node_t *node, my_bool_t *b)
{
    if (node->type != YAML_SCALAR_NODE) {
        return MY_FALSE;
    }

    int64_t len = (int64_t)node->data.scalar.length;
    char *data = (char *)node->data.scalar.value;

    if (len == sizeof("true") - 1 &&
        strncasecmp(data, "true", (size_t)len) == 0) {
        *b = MY_TRUE;
        return MY_TRUE;
    }

    if (len == sizeof("false") - 1 &&
        strncasecmp(data, "false", (size_t)len) == 0) {
        *b = MY_FALSE;
        return MY_TRUE;
    }

    return MY_FALSE;
}

static inline my_bool_t
my_yaml_node_str(yaml_node_t *node, my_str_t *str)
{
    if (node->type != YAML_SCALAR_NODE) {
        return MY_FALSE;
    }

    str->len = (int64_t)node->data.scalar.length;
    str->data = (char *)node->data.scalar.value;
    return MY_TRUE;
}